package com.qingke.business;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.ibatis.session.SqlSession;
import com.qingke.Command.UserCommand;
import com.qingke.mapper.RecordeMapper;
import com.qingke.memo.MemoConsole;
import com.qingke.memo.MemoDao;
import com.qingke.sqlclass.Recorde;

public class CreateCommand extends UserCommand{

	@Override
	public void executeUserCommand() {
		System.out.println("欢迎进入备忘录创建页面：");
		String title = MemoConsole.askUserInput("请输入标签【1.工作】【2.生活】【3.业余】请输入数字：>");
		String description = MemoConsole.askUserInput("请输入内容：>");
		String remind_time = MemoConsole.askUserInput("请输入所需提醒的时间(格式：yyyy-MM-dd HH:mm:ss):>");
		DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date myDate2 = null;
		try {
			myDate2 = dateFormat2.parse(remind_time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("提醒时间为："+myDate2);
		Calendar date = Calendar.getInstance();
		int year = date.get(Calendar.YEAR);
		int month = date.get(Calendar.MONTH) + 1;
		int day = date.get(Calendar.DAY_OF_MONTH);
		int hour = date.get(Calendar.HOUR_OF_DAY); 
		int minute = date.get(Calendar.MINUTE); 
		int second = date.get(Calendar.SECOND);
		String create_time = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
		System.out.println("创建时间为："+create_time);
		Date myDate3 = null;
		try {
			myDate3 = dateFormat2.parse(create_time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SqlSession session = MemoDao.mybatisSession();
		RecordeMapper recordeM = session.getMapper(RecordeMapper.class);
		Recorde recorde = new Recorde();
		recorde.setTitle(title);
		recorde.setDescription(description);
		recorde.setRemindTime(myDate2);
		recorde.setCreateTime(myDate3);
		recordeM.insert(recorde);
		session.commit();
		session.close();
		System.out.println("备忘录创建成功！");
	}


}
